package cn.tansanqinger.dao.sql.disposeresult;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.tansanqinger.dao.sql.IDisposeResultSqlDao;
import cn.tansanqinger.dao.sql.IQueryKnowledgeSqlDao;
import cn.tansanqinger.dao.sql.IQueryTopicSqlDao;
import cn.tansanqinger.dao.sql.IReportTopicSqlDao;
import cn.tansanqinger.pojo.Disposeresult;
import cn.tansanqinger.pojo.Queryknowledge;
import cn.tansanqinger.pojo.Querytopic;
import cn.tansanqinger.pojo.Reporttopic;
import cn.tansanqinger.util.PageData;

@Repository
public class DisposeResultSqlDaoImpl {
	
	@Autowired
	private IDisposeResultSqlDao dao;
	@Autowired
	private IReportTopicSqlDao tdao;
	@Autowired
	private IQueryKnowledgeSqlDao kqdao;
	@Autowired
	private IQueryTopicSqlDao tqdao;
//	@Autowired
//	private IUserMessage message;
	
	/**
	 * 查询被处理的信息
	 * @param id
	 * @param page
	 * @return
	 */
	public List<Disposeresult> selectDisposeresults(int id , int page){
		QueryWrapper<Disposeresult> queryWrapper = new QueryWrapper<Disposeresult>();
		queryWrapper.lambda().eq(Disposeresult::getUid, id);
		Page<Disposeresult> pd = new Page<Disposeresult>(page, 10);
		List<Disposeresult> disposeresults = dao.selectPage(pd, queryWrapper).getRecords();
		for (int i = 0; i < disposeresults.size(); i++) {
			if(disposeresults.get(i).getIsselect()==0) {
				disposeresults.get(i).setIsselect(1);
				dao.updateById(disposeresults.get(i));
			}
			if(disposeresults.get(i).getDtype()==0) {
				disposeresults.get(i).setDmessage(kqdao.selectById(disposeresults.get(i).getOid()).getQmessage());
			}
			if(disposeresults.get(i).getDtype()==1) {
				disposeresults.get(i).setDmessage(tqdao.selectById(disposeresults.get(i).getOid()).getQmessage());
			}
			if(disposeresults.get(i).getDtype()==2) {
				disposeresults.get(i).setDmessage(tdao.selectById(disposeresults.get(i).getOid()).getRmessage());
			}
		}
		return disposeresults;
	}
	
	/**
	 * 查询数据量多少
	 * @param id
	 * @return
	 */
	public int selectNumberAll(int id) {
		QueryWrapper<Disposeresult> queryWrapper = new QueryWrapper<Disposeresult>();
		queryWrapper.lambda().eq(Disposeresult::getUid, id);
		return dao.selectCount(queryWrapper);
	}
	
	/**
	 * 查询未读数据多少条
	 * @param id
	 * @return
	 */
	public int selectIsSelectNumber(int id) {
		QueryWrapper<Disposeresult> queryWrapper = new QueryWrapper<Disposeresult>();
		queryWrapper.lambda().eq(Disposeresult::getUid, id).eq(Disposeresult::getIsselect, 0);
		return dao.selectCount(queryWrapper);
	}
	
	/**
	 * 通过前端点击的数据返回相对应的结果
	 * @param did
	 * @return
	 */
	public PageData selectResultOne(int did) {
		PageData pd = new PageData();
		Disposeresult disposeresult = dao.selectById(did);
		if(disposeresult!=null) {
			pd.put("count", 0);
			pd.put("msg", "查询成功");
			if(disposeresult.getDtype()==0) {
				Queryknowledge queryknowledge = kqdao.selectById(disposeresult.getOid());;
				if(queryknowledge!=null) {
					pd.put("startDate",queryknowledge.getQtime());
					pd.put("endDate", disposeresult.getDdate());
					pd.put("qmessage", queryknowledge.getQmessage());
					pd.put("dmessage", queryknowledge.getQdisposemessage());
				} else {
					pd.put("count", -1);
					pd.put("msg", "数据错误");
				}
			} else if(disposeresult.getDtype()==1) {
				Querytopic querytopic = tqdao.selectById(disposeresult.getOid());;
				if(querytopic!=null) {
					pd.put("startDate",querytopic.getQtime());
					pd.put("endDate", disposeresult.getDdate());
					pd.put("qmessage", querytopic.getQmessage());
					pd.put("dmessage", querytopic.getQdisposemessage());
				} else {
					pd.put("count", -1);
					pd.put("msg", "数据错误");
				}
			} else if(disposeresult.getDtype()==2) {
				Reporttopic reporttopic = tdao.selectById(disposeresult.getOid());;
				if(reporttopic!=null) {
					pd.put("startDate",reporttopic.getRtime());
					pd.put("endDate", disposeresult.getDdate());
					pd.put("qmessage", reporttopic.getRmessage());
					pd.put("dmessage", reporttopic.getRdisposemessage());
				} else {
					pd.put("count", -1);
					pd.put("msg", "数据错误");
				}
			}
		} else {
			pd.put("count", -1);
			pd.put("msg", "数据错误");
		}
		return pd;
	}
	
	/**
	 * 删除数据
	 * @param id
	 * @return
	 */
	public int delete(int id) {
		return dao.deleteById(id);
	}

}
